package com.wilmer.question.service.impl;

import java.util.Iterator;
import java.util.List;

import com.wilmer.common.core.domain.entity.SysUser;
import com.wilmer.common.exception.ServiceException;
import com.wilmer.common.utils.DateUtils;
import com.wilmer.common.utils.SecurityUtils;
import com.wilmer.common.utils.StringUtils;
import com.wilmer.common.utils.bean.BeanValidators;
import com.wilmer.options.domain.YwOptions;
import com.wilmer.question.domain.YwQuestionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wilmer.question.mapper.YwQuestionMapper;
import com.wilmer.question.domain.YwQuestion;
import com.wilmer.question.service.IYwQuestionService;

/**
 * 题库题干Service业务层处理
 *
 * @author zl
 * @date 2024-04-16
 */
@Service
public class YwQuestionServiceImpl implements IYwQuestionService {
    @Autowired
    private YwQuestionMapper ywQuestionMapper;

    @Override
    public YwQuestion selectYwQuestionByQuestionId(Long questionId) {
        return this.ywQuestionMapper.selectYwQuestionByQuestionId(questionId);
    }

    @Override
    public String importQuestion(List<YwQuestionVO> list, Boolean isUpdateSupport, String operName) {
        if (!StringUtils.isNull(list) && list.size() != 0) {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder successMsg = new StringBuilder();
            StringBuilder failureMsg = new StringBuilder();
            Iterator var8 = list.iterator();

            while(var8.hasNext()) {
                YwQuestionVO question = (YwQuestionVO)var8.next();
                System.err.println(question.getYwOptionsList().size());
                Iterator var10 = question.getYwOptionsList().iterator();

                while(var10.hasNext()) {
                    YwOptions options = (YwOptions)var10.next();
                    System.err.println(options.toString());
                }
            }

            if (failureNum > 0) {
                failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                throw new ServiceException(failureMsg.toString());
            } else {
                successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                return successMsg.toString();
            }
        } else {
            throw new ServiceException("导入题库数据不能为空！");
        }
    }

    @Override
    public List<YwQuestion> selectYwQuestionList(YwQuestion ywQuestion) {
        return this.ywQuestionMapper.selectYwQuestionList(ywQuestion);
    }

    @Override
    public int insertYwQuestion(YwQuestion ywQuestion) {
        ywQuestion.setCreateTime(DateUtils.getNowDate());
        return this.ywQuestionMapper.insertYwQuestion(ywQuestion);
    }

    @Override
    public int updateYwQuestion(YwQuestion ywQuestion) {
        ywQuestion.setUpdateTime(DateUtils.getNowDate());
        return this.ywQuestionMapper.updateYwQuestion(ywQuestion);
    }

    @Override
    public int deleteYwQuestionByQuestionIds(Long[] questionIds) {
        return this.ywQuestionMapper.deleteYwQuestionByQuestionIds(questionIds);
    }

    @Override
    public int deleteYwQuestionByQuestionId(Long questionId) {
        return this.ywQuestionMapper.deleteYwQuestionByQuestionId(questionId);
    }
//    /**
//     * 查询题库题干
//     *
//     * @param questionId 题库题干主键
//     * @return 题库题干
//     */
//    @Override
//    public YwQuestion selectYwQuestionByQuestionId(Long questionId) {
//        return ywQuestionMapper.selectYwQuestionByQuestionId(questionId);
//    }
//
//    @Override
//    public String importQuestion(List<YwQuestionVO> list, Boolean isUpdateSupport, String operName) {
//        if (StringUtils.isNull(list) || list.size() == 0) {
//            throw new ServiceException("导入题库数据不能为空！");
//        }
//        int successNum = 0;
//        int failureNum = 0;
//        StringBuilder successMsg = new StringBuilder();
//        StringBuilder failureMsg = new StringBuilder();
//        for (YwQuestionVO question : list) {
////            try {
//                System.err.println(question.getYwOptionsList().size());
//                for (YwOptions options : question.getYwOptionsList()) {
//                    System.err.println(options.toString());
//                }
////                // 验证是否存在这个用户
////                SysUser u = userMapper.selectUserByUserName(user.getUserName());
////                if (StringUtils.isNull(u)) {
////                    BeanValidators.validateWithException(validator, user);
////                    user.setPassword(SecurityUtils.encryptPassword(password));
////                    user.setCreateBy(operName);
////                    userMapper.insertUser(user);
////                    successNum++;
////                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
////                } else if (isUpdateSupport) {
////                    BeanValidators.validateWithException(validator, user);
////                    checkUserAllowed(u);
////                    checkUserDataScope(u.getUserId());
////                    user.setUserId(u.getUserId());
////                    user.setUpdateBy(operName);
////                    userMapper.updateUser(user);
////                    successNum++;
////                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");
////                } else {
////                    failureNum++;
////                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
////                }
////            } catch (Exception e) {
////                failureNum++;
////                String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败：";
////                failureMsg.append(msg + e.getMessage());
////            }
//        }
//        if (failureNum > 0) {
//            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
//            throw new ServiceException(failureMsg.toString());
//        } else {
//            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
//        }
//        return successMsg.toString();
//    }
//
//    /**
//     * 查询题库题干列表
//     *
//     * @param ywQuestion 题库题干
//     * @return 题库题干
//     */
//    @Override
//    public List<YwQuestion> selectYwQuestionList(YwQuestion ywQuestion) {
//        return ywQuestionMapper.selectYwQuestionList(ywQuestion);
//    }
//
//    /**
//     * 新增题库题干
//     *
//     * @param ywQuestion 题库题干
//     * @return 结果
//     */
//    @Override
//    public int insertYwQuestion(YwQuestion ywQuestion) {
//        ywQuestion.setCreateTime(DateUtils.getNowDate());
//        return ywQuestionMapper.insertYwQuestion(ywQuestion);
//    }
//
//    /**
//     * 修改题库题干
//     *
//     * @param ywQuestion 题库题干
//     * @return 结果
//     */
//    @Override
//    public int updateYwQuestion(YwQuestion ywQuestion) {
//        ywQuestion.setUpdateTime(DateUtils.getNowDate());
//        return ywQuestionMapper.updateYwQuestion(ywQuestion);
//    }
//
//    /**
//     * 批量删除题库题干
//     *
//     * @param questionIds 需要删除的题库题干主键
//     * @return 结果
//     */
//    @Override
//    public int deleteYwQuestionByQuestionIds(Long[] questionIds) {
//        return ywQuestionMapper.deleteYwQuestionByQuestionIds(questionIds);
//    }
//
//    /**
//     * 删除题库题干信息
//     *
//     * @param questionId 题库题干主键
//     * @return 结果
//     */
//    @Override
//    public int deleteYwQuestionByQuestionId(Long questionId) {
//        return ywQuestionMapper.deleteYwQuestionByQuestionId(questionId);
//    }
}
